/**
 * @(#)CodeEntityInsideRelation.java, 2008-4-30.
 * Copyright 2008  Program Analysis Research Group(PARG),Institute of Software, Peking University.
 * All rights reserved.
 */
package edu.pku.sei.pgie.analyzer.internal.relation;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.TypeDeclaration;

import edu.pku.sei.pgie.analyzer.internal.CodeEntitySymbolTable;
import edu.pku.sei.pgie.analyzer.internal.VisitorWithContext;

/**
 * @author HeLi
 */
public class CodeEntityInsideRelation extends AbstractCodeEntityRelation {

    class InsideVisitor extends VisitorWithContext {
        List<String[]> container;

        public InsideVisitor() {
            super(false);
            this.container = new ArrayList<String[]>();
        }

        public List<String[]> getResult() {
            return container;
        }

        public boolean visit(TypeDeclaration node) {
            String parentType = peekTypeFQN();
            super.visit(node);
            String typeName = peekTypeFQN();
            if (parentType != null && typeName != null) {
                container.add(new String[] {
                    typeName, parentType
                });
            }
            return true;
        }

    }

    public String[][] collectCallBack(CompilationUnit root,
            CodeEntitySymbolTable symbolTable) throws Exception {
        InsideVisitor visitor = new InsideVisitor();
        root.accept(visitor);
        List<String[]> result = visitor.getResult();
        return result.toArray(new String[result.size()][]);
    }
}
